package com.poker.controller;

import com.poker.entity.ManagerInfo;
import com.poker.entity.ManagerMenu;
import com.poker.entity.Role;
import com.poker.service.ManagerInfoService;
import com.poker.service.ManagerMenuService;
import com.poker.service.RoleService;
import com.poker.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: 管理员账号管理;
**/
@Controller
@RequestMapping("/managerInfo")
public class ManagerInfoController extends BaseController {

    @Resource
    private RoleService roleService;

    @Resource
    private ManagerMenuService menuService;

    @Resource
    private ManagerInfoService managerInfoService;

    @RequestMapping("/list")
    public String list(Model model, ManagerInfo managerInfo,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Page<ManagerInfo> data = managerInfoService.findManagerInfoNoCriteria(page, size);
        model.addAttribute("data", data);
        model.addAttribute("managerInfo", managerInfo);
        return managerPage("managerInfo/list");
    }

    @RequestMapping("/findManagerInfoQuery")
    public String findManagerInfoQuery(Model model , ManagerInfo managerInfo,
                                @RequestParam(defaultValue = "0") Integer page,
                                @RequestParam(defaultValue = "10") Integer size) {
        Page<ManagerInfo> data = managerInfoService.findManagerInfoCriteria(page, size, managerInfo);
        model.addAttribute("data", data);
        model.addAttribute("managerInfo", managerInfo);
        return managerPage("managerInfo/list");
    }

    @RequestMapping("/delete")
    public String delete(String id, Integer page) {
        managerInfoService.deleteById(id);
        return redirect("/managerInfo/list?page=" + page);
    }

    @RequestMapping("/edit")
    public String edit(Model model, String id) {
        ManagerInfo managerInfo = new ManagerInfo();

        if (Misc.isNotEmpty(id)) managerInfo = managerInfoService.getById(id);

        model.addAttribute("managerInfo", managerInfo);
        return managerPage("managerInfo/edit");
    }

    @RequestMapping("/add")
    public String add(ManagerInfo managerInfo, String roles) {
        try {
            String[] array = roles.split(",");

            Set<Role> set = new HashSet<>();
            for (String anArray : array) { set.add(roleService.getById(anArray)); }

            managerInfo.setRoles(set);
            String salt = Misc.randomCode(16);
            managerInfo.setSalt(salt);
            managerInfo.setPassWord(Misc.getShiroMd5Pwd(managerInfo.getPassWord(), salt, 2));
            managerInfoService.save(managerInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return redirect("/managerInfo/list");
    }

    @ResponseBody
    @RequestMapping("/getMenu")
    public String getMenu() {
        List<ManagerMenu> data = menuService.findAll();

        return toJson(true, "success", data);
    }
}
